package bg.uni_sofia.conf_manager.beans;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.quartz.SchedulerException;

import bg.uni_sofia.conf_manager.mail.Quartz;
import bg.uni_sofia.conf_manager.model.Admin;
import bg.uni_sofia.conf_manager.model.Company;
import bg.uni_sofia.conf_manager.model.Conference;
import bg.uni_sofia.conf_manager.model.Employee;
import bg.uni_sofia.conf_manager.model.Lecture;
import bg.uni_sofia.conf_manager.model.Lecturer;

@Singleton
@Startup
public class TestDataInserter {

	@PersistenceContext
	private EntityManager em;

	@EJB
	private CompanyBean companyBean;

	@PostConstruct
	public void insertTestData() {
		// if (companyBean.getAllCompanies().size() == 0) {
		// first version
		// Company company1 = new Company("AAA", "jagduwgef", true,
		// new ArrayList<Employee>());
		// Company company2 = new Company("BBBB", "isgdbjrebsghbef", true,
		// new ArrayList<Employee>());
		// Admin admin1 = new Admin("Hor", "123");
		// Admin admin2 = new Admin("gfaga", "gaga");
		// Company comp = new Company("FMI", "Sucks", true,
		// new ArrayList<Employee>());
		// Date date = new Date(1402125951);
		// Employee employee = new Employee("nikola", "bla", "bla@bla.com",
		// "bla",
		// company1, true, date);
		// List<Lecture> lecturesList = new ArrayList<Lecture>();
		// List<Conference> conferencesList = new ArrayList<Conference>();
		// Lecturer lecturer = new Lecturer("nikola",
		// "09A61E3F41E0E1A17B76198027D1DD5093BDF093", "bla@bla.com",
		// "Joseph", "I wish I could split seas", lecturesList,
		// conferencesList);
		//
		// em.persist(lecturer);
		// em.persist(comp);
		// Admin admin3 = new Admin("qwe", "qea");
		// List<Lecture> lectures = new ArrayList<Lecture>();
		// Conference conference1 = new Conference("New Conference", "Vratsa",
		// new Date(1402018108), new Date(1402318108), employee, lectures);
		// Conference conference2 = new Conference("Second Conference",
		// "Burgas",
		// new Date(1402018108), new Date(1402318108), employee, lectures);
		// Lecture lecture1 = new Lecture("Lecture 1",
		// "This will be my first lecture", conference1, 12, new Date(),
		// new Date(), "19:00", new Date(), 1, lecturer);
		// Lecture lecture2 = new Lecture("Lecture 2",
		// "This will be my second lecture", conference2, 12, new Date(),
		// new Date(), "18:00", new Date(), 2, lecturer);
		//
		// lectures.add(lecture1);
		// lectures.add(lecture2);
		//
		//
		// em.persist(conference1);
		// em.persist(conference2);
		// em.persist(lecture1);
		// em.persist(lecture2);
		// em.persist(admin3);
		// em.persist(company1);
		// em.persist(company2);
		// em.persist(admin1);
		// em.persist(admin2);
		// em.persist(employee);
		// // }

		// teo version
		Company company1 = new Company("AAA", "jagduwgef", true,
				new ArrayList<Employee>());
		Company company2 = new Company("BBBB", "isgdbjrebsghbef", true,
				new ArrayList<Employee>());
		Employee employee = null;
		try {
			employee = new Employee("mario",
					LecturerBean.hashPassword("1234"), "bla@bla.com",
					"bla", company1, true, new Date());
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Conference conference1 = new Conference("New Conference", "Vratsa",
				new Date(), new Date(), employee, new ArrayList<Lecture>(), new ArrayList<Lecturer>());
		Conference conference2 = new Conference("New Conference2", "Burgas",
				new Date(), new Date(), employee, new ArrayList<Lecture>(), new ArrayList<Lecturer>());
		Conference conference3 = new Conference("New Conference3", "Sofia",
				new Date(), new Date(), employee, new ArrayList<Lecture>(), new ArrayList<Lecturer>());
		Lecturer lecturer = null;
		try {
			lecturer = new Lecturer("nikola",
					LecturerBean.hashPassword("1234"), "bla@bla.com", "Joseph",
					"I wish I could split seas", new ArrayList<Lecture>(),
					new ArrayList<Conference>());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Admin admin = null;
		try {
			admin = new Admin("admin", LecturerBean.hashPassword("admin"));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Lecture lecture = new Lecture("aaaaa", "cerg", conference1, 100,
				new Date(), new Date(), "10:10", new Date(), 0, lecturer);
		Lecture lecture2 = new Lecture("aaaaa2", "cerg2", conference1, 200,
				new Date(), new Date(), "20:10", new Date(), 1, lecturer);

		Lecture lecture3 = new Lecture("aaaaa3", "cerg3", conference2, 300,
				new Date(), new Date(), "10:10", new Date(), 0, lecturer);
		Lecture lecture4 = new Lecture("aaaaa4", "cerg4", conference2, 400,
				new Date(), new Date(), "20:10", new Date(), -1, lecturer);

		Lecture lecture5 = new Lecture("aaaaa5", "cerg5", conference3, 500,
				new Date(), new Date(), "20:50", new Date(), 1, lecturer);
		Lecture lecture6 = new Lecture("aaaaa6", "cerg6", conference2, 500,
				new Date(), new Date(), "20:50", new Date(), 1, lecturer);

		conference1.addLecture(lecture);
		conference1.addLecture(lecture2);
		conference2.addLecture(lecture3);
		conference2.addLecture(lecture4);
		conference2.addLecture(lecture6);
		conference3.addLecture(lecture5);
		lecturer.addConference(conference1);
		lecturer.addConference(conference2);
		lecturer.addConference(conference3);
		conference1.addLecturer(lecturer);
		conference2.addLecturer(lecturer);
		conference3.addLecturer(lecturer);
		em.persist(admin);
		em.persist(company1);
		em.persist(company2);
		em.persist(employee);
		em.persist(lecture);
		em.persist(lecture2);
		em.persist(lecture3);
		em.persist(lecture4);
		em.persist(lecture5);
		em.persist(lecture6);
		em.persist(conference1);
		em.persist(conference2);
		em.persist(conference3);
		em.persist(lecturer);

		// nikolaaaaaa version
		// Company company1 = new Company("AAA", "jagduwgef", true,
		// new ArrayList<Employee>());
		// Company company2 = new Company("BBBB", "isgdbjrebsghbef", true,
		// new ArrayList<Employee>());
		// Admin admin1 = new Admin("Hor", "123");
		// Admin admin2 = new Admin("gfaga", "gaga");
		// Company comp = new Company("FMI", "Sucks", true,
		// new ArrayList<Employee>());
		// Date date = new Date(1402125951);
		// Employee employee = new Employee("nikola", "bla", "bla@bla.com",
		// "bla",
		// company1, true, date);
		// List<Lecture> lecturesList = new ArrayList<Lecture>();
		// List<Conference> conferencesList = new ArrayList<Conference>();
		// Lecturer lecturer = null;
		// try {
		// lecturer = new Lecturer("nikola",
		// LecturerBean.hashPassword("1234"), "bla@bla.com", "Joseph",
		// "I wish I could split seas", lecturesList, conferencesList);
		// } catch (NoSuchAlgorithmException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// em.persist(lecturer);
		// em.persist(comp);
		// Admin admin3 = new Admin("qwe", "qea");
		// List<Lecture> lectures = new ArrayList<Lecture>();
		// Conference conference1 = new Conference("New Conference", "Vratsa",
		// new Date(1402018108), new Date(1402318108), employee, lectures);
		// Conference conference2 = new Conference("Second Conference",
		// "Burgas",
		// new Date(1402018108), new Date(1402318108), employee, lectures);
		// Lecture lecture1 = new Lecture("Lecture 1",
		// "This will be my first lecture", conference1, 12, new Date(),
		// new Date(), "19:00", new Date(), 1, lecturer);
		// Lecture lecture2 = new Lecture("Lecture 2",
		// "This will be my second lecture", conference2, 12, new Date(),
		// new Date(), "18:00", new Date(), 2, lecturer);
		//
		// lectures.add(lecture1);
		// lectures.add(lecture2);
		// em.persist(conference1);
		// em.persist(conference2);
		// em.persist(lecture1);
		// em.persist(lecture2);
		// em.persist(admin3);
		// em.persist(company1);
		// em.persist(company2);
		// em.persist(admin1);
		// em.persist(admin2);
		// em.persist(employee);
		// }
		
		try {
			Quartz.runScheduler();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
